<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }     
    public function login()
    {
		$this->form_validation->set_rules('username', 'Kullanıcı Adı', 'min_length[3]|required|max_length[12]|trim|xss_clean');
        $this->form_validation->set_rules('password', 'Şifre', 'required|trim|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('partials/page_login');
		}
		else
		{
		   // databaseden çek et
           //$this->load->model('user_model');
           $user_info = $this->user_model->try_login($this->input->post('username'),$this->input->post('password'));
           if(!$user_info)
           {
            // kull adi veya şifre hatalı döndür..
              $this->session->set_flashdata('error','Kullanıcı Adı veya Şifre hatalı!');
              redirect('user/login');
           }
           else
           {
               
               $sess_data = array(
                    'logged_in'     =>  true,
                    'user_id'       =>  $user_info[0]->user_id,
                    'username'      =>  $user_info[0]->username,
                    'name'          =>  $user_info[0]->name,
                    'surname'       =>  $user_info[0]->surname 
              );
              $this->session->set_userdata($sess_data);
		      redirect('main');
           }

		} 
  
    }
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('user/login');     
    }
	public function username_check($str)
	{
		if ($this->db->where('username', $str)->get('users')->num_rows()>0)
		{
			$this->form_validation->set_message('username_check', 'Bu kullanıcı adı kullanılıyor, lütfen başka bir kullanıcı adı seçiniz.');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	} 
	public function email_check($str)
	{
		if ($this->db->where('email', $str)->get('users')->num_rows()>0)
		{
			$this->form_validation->set_message('email_check', 'Bu E-posta adresi kullanılıyor, lütfen başka bir E-posta adresi seçiniz.');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}        
    public function register()
    {
		$this->form_validation->set_rules('username', 'Kullanıcı Adı', 'min_length[3]|required|max_length[12]|trim|xss_clean|callback_username_check');
        $this->form_validation->set_rules('password', 'Şifre', 'min_length[6]|required|trim|xss_clean');
        $this->form_validation->set_rules('name', 'İsim', 'required|trim|xss_clean');
        $this->form_validation->set_rules('surname', 'Soyad', 'required|trim|xss_clean');
        $this->form_validation->set_rules('email', 'E-posta', 'valid_email|required|trim|xss_clean|callback_email_check');
        $this->form_validation->set_rules('repassword', 'Şifre tekrarı', 'matches[password]|required|trim|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('partials/page_register');
		}
		else
		{
		    if($this->user_model->register_user(
                $this->input->post('username'),
                $this->input->post('password'),
                $this->input->post('name'),
                $this->input->post('surname'),
                $this->input->post('email')
            ))
            {
                    $sess_data = array(
                    'logged_in'     =>  true,
                    'user_id'       =>  $this->db->insert_id(),
                    'username'      =>  $this->input->post('username'),
                    'name'          =>  $this->input->post('name'),
                    'surname'       =>  $this->input->post('surname') 
              );
              $this->session->set_userdata($sess_data);
		      redirect('main');
            }
            else
            {
                // yazılamadı
            }
        } 
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */